package com.zjdiepu.www.rest.api.base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.commpent.helper.UserMapSalermanHelper;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoService;
import com.zjdiepu.www.ibiz.third.IJuheCityService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfo;
import com.zjdiepu.www.model.base.ext.DpBaseDealerInfoExt;
import com.zjdiepu.www.model.base.request.DealerInfoRequest;
import com.zjdiepu.www.model.base.response.DealerInfoResponse;

/**
 * 经销商表Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月21日 10时30分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DealerInfoController", produces="application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = {"经销商"}, produces="application/json;charset=UTF-8")
public class DealerInfoController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(DealerInfoController.class);
    @Autowired
    private IDpBaseDealerInfoService dpBaseDealerInfoService;
    @Resource
    private TokenUserHelper tokenUserHelper;
	@Resource
	private UserMapSalermanHelper userMapSalermanHelper;
	@Resource
	private IJuheCityService juheCityService;

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 经销商列表 <br/>
     * @param dealerInfoList
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "经销商", response = DealerInfoResponse.class, httpMethod="POST")
    @ApiImplicitParams({ //还款状态、当前逾期金额
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "dealerInfoList")
    public ApiResponse dealerInfoList(@Validated @RequestBody DealerInfoRequest dealerInfoRequest) {
    	tokenUserHelper.getUser();
    	DpBaseDealerInfoExt dpBaseDealerInfoExt = new DpBaseDealerInfoExt();
    	dpBaseDealerInfoExt.setBaseWhere();
    	dpBaseDealerInfoExt.setDealerName(dealerInfoRequest.getDealerName());
    	dpBaseDealerInfoExt.setOrderSql("`CREATED` DESC");
    	dpBaseDealerInfoExt.setPage(dealerInfoRequest.getPage());
    	dpBaseDealerInfoExt.setRows(dealerInfoRequest.getRows());
    	
    	PageVo page = getPage(dpBaseDealerInfoExt);
    	int records = dpBaseDealerInfoService.countByExt(dpBaseDealerInfoExt);
		page.setRecords(records);
		if (records > 0) {
			List<DpBaseDealerInfo> dpBaseDealerInfos = dpBaseDealerInfoService.selectByExt(dpBaseDealerInfoExt);
			List<DealerInfoResponse> dealerInfoResponses = mapDpBaseDealerInfos(dpBaseDealerInfos);
			page.setRows(dealerInfoResponses);
		} else {
			page.setRows(CollectionInitUtil.emptyList());
		}
        return ApiResponse.success(page);
    }
    
    /**
     * @author zhangzj <br/>
     * @Title: save  <br/>
     * @Description: 保存经销商 <br/>
     * @param dealerInfoList
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "保存经销商", response = DealerInfoResponse.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "saveDealerInfo")
    public ApiResponse saveDealerInfo(@RequestBody DpBaseDealerInfo dpBaseDealerInfo) {
    	dpBaseDealerInfo.setDealerLevel("B");
    	dpBaseDealerInfoService.saveUpdateAndDelete(dpBaseDealerInfo, OperateType.add);
    	return ApiResponse.fail("999","该功能暂未开放");
//        return ApiResponse.success(true);
    }
    
    
//  ============================================辅助方法===========================================
    
  /**
   * 转换属性：展示
   * @param dpBaseDealerInfos
   * @return
   */
  private List<DealerInfoResponse> mapDpBaseDealerInfos(List<DpBaseDealerInfo> dpBaseDealerInfos){
	  if(CollectionUtils.isEmpty(dpBaseDealerInfos)) {
	  		return CollectionInitUtil.emptyList();
	  	}
	  	List<DealerInfoResponse> dealerInfoResponses = CollectionInitUtil.emptyList();
	  	for(DpBaseDealerInfo dpBaseDealerInfo : dpBaseDealerInfos) {
	  		DealerInfoResponse dealerInfoResponse = mapDpBaseDealerInfo(dpBaseDealerInfo);
	  		if(dealerInfoResponse != null) {
	  			dealerInfoResponses.add(dealerInfoResponse);
	  		}
	  	}
	  	return dealerInfoResponses;
  }
  
  /**
   * 转换属性：展示
   * @param dpBaseDealerInfo
   * @return
   */
  private DealerInfoResponse mapDpBaseDealerInfo(DpBaseDealerInfo dpBaseDealerInfo){
  	if(dpBaseDealerInfo == null) {
  		return null;
  	}
  	try {
		DealerInfoResponse dealerInfoResponse = new DealerInfoResponse();
		BeanUtils.copyProperties(dealerInfoResponse, dpBaseDealerInfo);
		return dealerInfoResponse;
	} catch (IllegalAccessException | InvocationTargetException e) {
		logger.error("对象拷贝属性出错.", e);
		return null;
	}
  }

}
